package com.chat.view;

import com.chat.Local;
import com.chat.service.ManageInviteFriend;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import java.io.IOException;
import java.util.ArrayList;

import static com.chat.view.MainWindowController.chat;

public class InviteController {
    public ListView invitefriendlist;
    public Label tishi;

    public static ObservableList<InviteFriendData> in= FXCollections.observableArrayList();

    public static ArrayList<String>a=new ArrayList<>();

    public void initialize() {
           invitefriendlist.getItems().clear();
           in.clear();
           a.clear();
           invitefriendlist.setItems(in);
           if(Local.friendlist.size()==0){
                tishi.setText("当前没有可邀请的好友");
           }else{
               tishi.setText("");
           }
           for(int i=0;i<Local.friendlist.size();i++){
                 in.add(new InviteFriendData(Local.friendlist.get(i).getNickname(),Local.friendlist.get(i).getUsername(),Local.friendlist.get(i).getQianming()));
                  a.add(Local.friendlist.get(i).getUsername());
           }
        invitefriendlist.setCellFactory(new Callback<ListView, ListCell>() {
            @Override
            public ListCell call(ListView param) {
                return new ListCell<InviteFriendData>(){
                    @Override
                    protected void updateItem(InviteFriendData item, boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            HBox hbox=new HBox();
                            FXMLLoader fxmlLoader=new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("invitefriend.fxml"));
                            try {
                                hbox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            setOnMouseClicked(event ->{

                            });
                            InviteFriendController inviteFriendController=fxmlLoader.getController();
                            hbox.setPadding(new Insets(20,0,0,0));
                            hbox.setPrefHeight(50);
                             inviteFriendController.setNickname(item.nickname);
                             inviteFriendController.setTouxiang(item.username);
                             inviteFriendController.setQianming(item.qianming);
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    ManageInviteFriend.add(item.username,inviteFriendController);}
                            });
                            Platform.runLater(()-> {
                                this.setGraphic(hbox);
                            });
                        }
                        else {
                            Platform.runLater(()-> {
                                this.setGraphic(null);
                            });

                        }
                    }
                };
            }
        });
    }
        public void Invite(MouseEvent mouseEvent) {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                     Alert alert = new Alert(Alert.AlertType.INFORMATION);
                     alert.setTitle("提示");
                     alert.setHeaderText("恭喜");
                     alert.setContentText("邀请成功！");
                     alert.showAndWait();
                     Stage stage=(Stage) invitefriendlist.getScene().getWindow();
                            stage.close();
                }
            });

    }
}
